from flask import Flask, request, jsonify
from flask_cors import CORS
from scipy.signal import savgol_filter

app = Flask(__name__)
CORS(app)  # 允许所有源的跨域请求

@app.route('/api/process', methods=['POST'])
def process_data():
    data = request.json
    window_length = int(data['window_length'])
    polyorder = int(data['polyorder'])
    mode = data['mode']
    wavelengths = data['wavelengths']
    intensities = data['intensities']
    # print(type(data))
    # print(type(window_length))
    # print(type(mode))
    # print(type(wavelengths[0]))
    # print(type(intensities[0]))


    filtered_intensities = savgol_filter(intensities, window_length, polyorder, mode=mode).tolist()

    return jsonify({
        'wavelengths': wavelengths,
        'intensities': filtered_intensities
    })

if __name__ == '__main__':
    app.run(port=5000, debug=True)
